#include "_RecvBusData_proc.h"


u32 _RecvBusData_proc(B1553_BusService *pBusServ)
{
    u16 foo = 0, lenth = 0;
    u32 ret = 0;
    struct B1553_TmPacket *pTmData = &(pBusServ->Telemeter_TX_Data);
    struct BusCmdData *pbusData = &(pBusServ->TC_CmdData);
    // 根据DTD字节数量收数据
    rt_GetBusCMDData(pBusServ);
    // 数据转换并判断
    lenth = 7 + pbusData->packLenth;              // 解析指令码中的字节数
    if (lenth != pBusServ->DistrbTransDesc_bytes) // 包长度错误 函数返回
    {
        pTmData->Comm_Error_Flag = TELEMETRY_BUSCMD_LENTH_ERR; // 遥测报错
        pTmData->Comm_Error_Count++;                           // 错误计数+1
        return CMDHANDLE_DONE;
    }

    // 判断CRC校验和
    foo = iso_8473_checksum((u8 *)pbusData, (lenth - 2)); // 扣除2字节CRC的值
    lenth = ((lenth - 14) >> 1) - 1;                      // 根据字节长度确定CRC的位置 lenth减去包头除以2减1得到CRC位置
    if (foo != pbusData->data[lenth])
    {
        pTmData->Comm_Error_Flag = TELEMETRY_BUSCMD_ISOSUM_ERR; // 遥测报错
        pTmData->Comm_Error_Count++;                            // 错误计数+1
        return CMDHANDLE_DONE;
    }

    if (TC_BUSCOMMAND_ID == pbusData->packID) // 总线指令
    {
        pTmData->Comm_ZXZL_RX_Count++;
        ret = _BusCMDData_proc(pBusServ);
    }
    else if (TC_DATAINJECTION_ID == pbusData->packID) // 数据注入
    {
        pTmData->Comm_SJZR_RX_Count++;
        ret = _BusInjectData_proc(pBusServ);
    }
    else
    {
        pTmData->Comm_Error_Count++;
        pTmData->Comm_Error_Flag = TELEMETRY_BUSCMD_UNDEFINE_ERR;
        ret = CMDHANDLE_DONE;
    }

    return ret;
}